package com.mobile.challenge.web;
import com.mobile.challenge.core.Result;
import com.mobile.challenge.core.ResultGenerator;
import com.mobile.challenge.model.Prize;
import com.mobile.challenge.model.PrizeRecord;
import com.mobile.challenge.service.PrizeRecordService;
import com.mobile.challenge.service.PrizeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mobile.challenge.service.RuleTitleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Created by Lix on 2018/06/04.
*/
@Api(value = "奖品接口",tags = {"奖品记录管理"})
@RestController
@RequestMapping("/prize")
public class PrizeController {

    @Resource
    private PrizeService prizeService;

    @Resource
    private PrizeRecordService prizeRecordService;


    private Logger logger = LoggerFactory.getLogger(PrizeController.class);

    /**
     * 我的奖品
     * @param prizeRecord
     * @return
     */
    @RequestMapping(value = "/mine", method = RequestMethod.POST, produces = {"application/json"})
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "我的奖品", notes = "必须提供一个phoneNo,点击'我的奖品'请求此接口")
    public Result getPrizeInfo(@RequestBody PrizeRecord prizeRecord){

        //我的奖品单位M
        Map data = prizeRecordService.myPrize(prizeRecord.getPhoneNo());

        return ResultGenerator.genSuccessResult(data);
    }

    /**
     * 剩余多少个奖
     * @return
     */
    @RequestMapping(value = "/remaining/count", method = RequestMethod.GET, produces = {"application/json"})
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "剩余多少个奖")
    public Result getRemCount4Prize() {
        //剩余多少个奖
        int remainingCount = prizeService.findById(1).getPrizeTotal();
        Map data = new HashMap();
        data.put("count",remainingCount);
        return ResultGenerator.genSuccessResult(data);
    }

    /**
     * 获奖机会查询
     * @param prizeRecord
     * @return
     */
    @RequestMapping(value = "/chance", method = RequestMethod.POST, produces = {"application/json"})
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "是否有获奖机会", notes = "必须提供一个phoneNo,挑战成绩为10000时请求此接口")
    public Result chance(@RequestBody PrizeRecord prizeRecord){

        //是否有机会中奖
        Map data = prizeRecordService.findByPhoneNo(prizeRecord.getPhoneNo());

        return ResultGenerator.genSuccessResult(data);
    }
//    /**
//     * 添加一条中奖记录(带锁)
//     * @param prizeRecord
//     * @return
//     */
//    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = {"application/json"})
//    @ResponseStatus(HttpStatus.OK)
//    @ApiOperation(value = "添加一条中奖记录", notes = "必须提供一个phoneNo,点击'开始挑战'请求此接口")
//    public synchronized Result  addPrizeRecord(@RequestBody PrizeRecord prizeRecord) {
//        Map data = new HashMap();
//        Map rs = prizeRecordService.add(prizeRecord);
//        if(!(boolean)rs.get("code")){
//            return ResultGenerator.genFailResult((String) rs.get("msg"),data);
//        }
//
//        return ResultGenerator.genSuccessResult(data);
//    }
}
